import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.HibernateException;
import org.hibernate.Criteria;
import org.hibernate.cfg.AnnotationConfiguration;
import dao.hibernate.Contact;
import dao.hibernate.Medications;

public class FirstExample {
  private static final SessionFactory ourSessionFactory;

  static {
    try {
      ourSessionFactory = new AnnotationConfiguration().
              configure("hibernate.cfg.xml").
              buildSessionFactory();
    }
    catch (Throwable ex) {
      throw new ExceptionInInitializerError(ex);
    }
  }

  public static Session getSession() throws HibernateException {
    return ourSessionFactory.openSession();
  }

  public static void main(String[] args) {
    final Session session = getSession();

    try{
        Criteria criteria = session.createCriteria(Medications.class);
			  //return criteria.list();
      //Create new instance of Contact and set values in it by reading them from form object
        System.out.println("Inserting Record");
        dao.hibernate.Contact contact = new Contact();
        contact.setId(3);
        contact.setFirstName("Deepak");
        contact.setLastName("Kumar");
        contact.setEmail("deepak_38@yahoo.com");
        session.save(contact);
        System.out.println("Done");
    }catch(Exception e){
      System.out.println(e.getMessage());
    }finally{
      // Actual contact insertion will happen at this step
      session.flush();
      session.close();
    }
  }
}
